#include "viewcontactdialog.h"

ViewContactDialog::ViewContactDialog(QWidget *parent, Qt::WFlags f)
    : QWidget(parent, f)
{
    // initialize the dialog
	setupUi(this);
	isCalling = true;
	callPushButton->setPalette(QPalette(Qt::green));
	v->setAlignment(Qt::AlignCenter);
	r->setAlignment(Qt::AlignCenter);
	b->setAlignment(Qt::AlignCenter);
	o->setAlignment(Qt::AlignCenter);
}

ViewContactDialog::~ViewContactDialog()
{
	qDebug() << "~ViewContactDialog";
    if (isCalling) emit stopCallContactSignal();
}

void ViewContactDialog::setupView(NeoPhoneBookEntry *entry)
{    
	nameLabel->setText(entry->getContactName());
	numberLabel->setText(entry->getPhoneNumber());
	emailLabel->setText(entry->getContactEmail());
	QString pictureFilePath = entry->getPictureFilePath();
	if (!pictureFilePath.isEmpty()) {
		QDir dir = QDir::home();
		dir.cd("Documents");
		QPixmap pixmap(dir.filePath(pictureFilePath));
		if (!pixmap.isNull()) {
			pixmap = pixmap.scaled(QSize(pictureLabel->width(), 
					 pictureLabel->height()), Qt::KeepAspectRatio, 
					 Qt::SmoothTransformation);
			pictureLabel->setPixmap(pixmap);
		}
	}
}

void ViewContactDialog::on_editPushButton_clicked()
{
	//emit stopCallContactSignal(); // emit by ~ViewContactDialog
	emit editContactSignal();
	close();
}
void ViewContactDialog::on_deletePushButton_clicked()
{
	emit stopCallContactSignal();
	QMessageBox msgBox;
    msgBox.setWindowTitle("Delete Contact");
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setText("Do you really want to do that?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);	
	
    switch (msgBox.exec()) {	
    case QMessageBox::Yes:
		//emit stopCallContactSignal(); // emit by ~ViewContactDialog
		emit deleteContactSignal();
		close();
		break;
    case QMessageBox::No:
        break;
    default:
        break;
    }	
}
void ViewContactDialog::on_callPushButton_clicked()
{	
	//if (isCalling) {
	//	isCalling = false;
	//	emit stopCallContactSignal();
	//	callPushButton->setText("Call");
	//	callPushButton->setPalette(QPalette(Qt::green));
	//} else {
	//	isCalling = true;
		emit callContactSignal();
	//	callPushButton->setText("Stop");
	//	callPushButton->setPalette(QPalette(Qt::red));
	//}	
}

void ViewContactDialog::vibrationListener(bool pos, int id)
{
	if (pos) {
		deviceOn(id);
	} else {
		deviceOff(id);
	}
}

void ViewContactDialog::deviceOn(int id)
{
	switch (id) {
	case 0:
		b->setStyleSheet("QLabel{background-color: #0000E3;}");
		break;
	case 1:
		o->setStyleSheet("QLabel{background-color: #FF5809;}");
		break;
	case 2:
		r->setStyleSheet("QLabel{background-color: #FF0000;}");
		break;
	case 3:
		v->setStyleSheet("QLabel{background-color: #8E8E8E;}");
		break;
	default:
		qDebug() << "Unknown device id";
		break;
	}
}

void ViewContactDialog::deviceOff(int id)
{
	switch (id) {
	case 0:
		b->setStyleSheet("QLabel{background-color: #000000;}");
		break;
	case 1:
		o->setStyleSheet("QLabel{background-color: #000000;}");
		break;
	case 2:
		r->setStyleSheet("QLabel{background-color: #000000;}");
		break;
	case 3:
		v->setStyleSheet("QLabel{background-color: #000000;}");
		break;
	default:
		qDebug() << "Unknown device id";
		break;
	}
}

void ViewContactDialog::setSlider(int duration)
{
	qDebug() << "set slider duration" << duration;
	slider->setMaximum(duration);
}

void ViewContactDialog::audioPlayingListenser(int offset)
{
	//qDebug() << "catch playing signal" << offset;
	slider->setSliderPosition(offset);
}

void ViewContactDialog::on_upPushButton_clicked()
{
	//qDebug() << "face up";
	emit stateChangeSignal(AccelManager::UP);
}

void ViewContactDialog::on_downPushButton_clicked()
{
	//qDebug() << "face down";
	emit stateChangeSignal(AccelManager::DOWN);
}

void ViewContactDialog::closeListener(int condition) 
{
	if (condition) isCalling = false;
	close();
}







